﻿using AspNetCoreRateLimit;

namespace WomanMotion.WebApi.Api.Limit
{
    /// <summary>
    /// 接口限流中间件配置
    /// </summary>
    public static class ConfigureRateLimit
    {
        public static void AddRateLimit(this IServiceCollection services, IConfiguration conf)
        {
            services.AddMemoryCache();
            services.Configure<IpRateLimitOptions>(conf.GetSection("IpRateLimiting"));
            services.AddInMemoryRateLimiting();
            services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
        }

        public static IApplicationBuilder UseRateLimit(this IApplicationBuilder app)
        {
            app.UseIpRateLimiting();
            //app.UseMiddleware<IPLimitMiddleware>();
            return app;
        }
    }
}